package com.zzyl.controller.customer;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.TimeSlotCount;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.DateTimeUtils;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeSlotCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
@Slf4j
public class CustomerReservationController extends BaseController {

    @Autowired
    private CustomerReservationService customerReservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询该用户已取消预约数量")
    public ResponseResult<Integer> getCancelledReservationCount() {
        try {
            int count = customerReservationService.getCancelledCount();
            return ResponseResult.success(count);
        } catch (Exception e) {
            log.error("查询已取消预约数量失败", e);
            return ResponseResult.error("查询失败");
        }
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public ResponseResult<List<TimeSlotCount>> getCountByTime(
            @RequestParam(required = false)  // 添加 required = false )
            Long time) {
        try {
            // 使用默认值
            if (time == null) {
                time = LocalDateTime.now()  // 当前完整时间（年-月-日 时:分:秒）
                        .atZone(ZoneId.systemDefault())  // 绑定系统时区
                        .toInstant()  // 转为Instant
                        .toEpochMilli();  // 转为毫秒级时间戳（long类型）
            }
            time = time / 1000;
            System.out.println(time);
            // 将时间戳转回LocalDateTime（完整日期+时间）并格式化输出
            String dateTime = DateTimeUtils.timestampToString(time); // （年-月-日 时:分:秒）
            System.out.println(dateTime);
            List<TimeSlotCountVo> result = customerReservationService.getCountByTime(time);
            return ResponseResult.success(result);
        } catch (Exception e) {
            log.error("查询时间段剩余预约次数失败", e);
            return ResponseResult.error("查询失败");
        }
    }

    @PostMapping
    @ApiOperation("新增预约")
    public ResponseResult<ResponseResult> addReservation(@RequestBody ReservationDto reservationDto) {
        try {
            customerReservationService.addReservation(reservationDto);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("预约失败", e);
            return ResponseResult.error("预约失败");
        }
    }

    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public ResponseResult<PageResponse<ReservationVo>> getReservationByPage(
            @ApiParam(value = " 预约状态（0：待报道，1：已完成，2：取消，3：过期）")
            @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "当前页码")
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示数量")
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResponse<ReservationVo> reservationVoPageInfo = customerReservationService.getByPage(status, pageNum, pageSize);
        return success(reservationVoPageInfo);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public ResponseResult cancelReservation(@PathVariable Long id) {
        try {
            customerReservationService.cancelReservation(id);  // 修改了拼写错误的 `customerReservationServcice`
            return ResponseResult.success("取消预约成功");
        } catch (Exception e) {
            log.error("取消预约失败", e);
            return ResponseResult.error("取消预约失败");
        }
    }
}